//Write a java program using nested try-catch blocks.

public class NestedTryCatch {
    public static void main(String[] args) {
        try {
            // Outer try block
            int[] numbers = { 10, 20, 30 };

            for (int i = 0; i <= numbers.length; i++) {
                try {
                    // Inner try block
                    int result = divideNumbers(numbers[i], i);
                    System.out.println("Result: " + result);
                } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {
                    handleException(e);
                }
            }
        } catch (Exception e) {
            System.out.println("Exception occurred: " + e.getMessage());
        }
    }

    static int divideNumbers(int dividend, int divisor) {
        return switch (divisor) {
            case 0 -> throw new ArithmeticException("Cannot divide by zero");
            case 1, 2 -> dividend / divisor;
            default -> throw new ArrayIndexOutOfBoundsException("Invalid index: " + divisor);
        };
    }

    static void handleException(Exception e) {
        if (e instanceof ArithmeticException) {
            System.out.println("Divide by zero error: " + e.getMessage());
        } else if (e instanceof ArrayIndexOutOfBoundsException) {
            System.out.println("Array index out of bounds: " + e.getMessage());
        } else {
            System.out.println("Unknown exception: " + e.getMessage());
        }
    }
}

